//
//  CUserSettingMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/25.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CUserSettingMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CUserSettingModel.h"
#import "CAccountMgr.h"
#import "CAccountModel.h"

@implementation CUserSettingMgr


//插入用户设置
-(BOOL)insetUserWithSetting:(CUserSettingModel *)setting
{
    if(setting == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(setting.userCode)) {
            return NO;
        }
    }

    

    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        FMResultSet *result = [db executeQuery:@"select * from USERSETTING where  userCode = ?",setting.userCode];
        if (result && [result next]) {
            ret = YES;
        }
        [result close];
        if (ret) {
            ret = [db executeUpdate:@"UPDATE USERSETTING SET Voice = ?, Shake = ?, Disturb = ?, StartDate = ?, EndDate = ?,BothOnline = ?,IsOnlyWifiRcv = ?,Autograph= ?,isUseGesturePassword = ?,isForgetGuesturePassword = ?, yuLiuZiduan2 = ?, yuLiuZiduan3 = ? where userCode = ?", setting.Voice?@1:@0, setting.Shake?@1:@0, setting.Disturb?@1:@0, M_STOREVALUE(setting.StartDate), M_STOREVALUE(setting.EndDate),setting.BothOnline?@1:@0,setting.IsOnlyWifiRcv?@1:@0,M_STOREVALUE(setting.Autograph),setting.isUseGesturePassword?@1:@0,setting.isForgetGuesturePassword?@1:@0,@(setting.yuLiuZiduan2),M_STOREVALUE(setting.yuLiuZiduan3),M_STOREVALUE(setting.userCode)];

        } else {
           ret = [db executeUpdate:@"INSERT INTO USERSETTING(Voice, Shake, Disturb, StartDate, EndDate,BothOnline,IsOnlyWifiRcv,Autograph,userCode,isUseGesturePassword,isForgetGuesturePassword,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?)", setting.Voice?@1:@0, setting.Shake?@1:@0, setting.Disturb?@1:@0, M_STOREVALUE(setting.StartDate), M_STOREVALUE(setting.EndDate),setting.BothOnline?@1:@0,setting.IsOnlyWifiRcv?@1:@0,M_STOREVALUE(setting.Autograph),M_STOREVALUE(setting.userCode),setting.isUseGesturePassword?@1:@0,setting.isForgetGuesturePassword?@1:@0,M_STOREVALUE(setting.yuLiuZiduan1),@(setting.yuLiuZiduan2),M_STOREVALUE(setting.yuLiuZiduan3)];
        }
        
    }];
    return ret;
}

//获取用户设置
- (CUserSettingModel *)getUserSetting
{
    CAccountModel *account = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    __block CUserSettingModel *setting = nil;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        NSString *sql = [NSString stringWithFormat:@"select * from USERSETTING where  userCode = \'%@\'", account.userName];
        FMResultSet *result =  [db  executeQuery:sql];
        if(result != nil)
        {
            while ([result next]) {
                setting = [[CUserSettingModel alloc] init];
                setting.Voice = [result boolForColumn:@"Voice"];
                setting.Shake = [result boolForColumn:@"Shake"];
                setting.Disturb = [result boolForColumn:@"Disturb"];
                setting.StartDate = [result stringForColumn:@"StartDate"];
                setting.EndDate = [result stringForColumn:@"EndDate"];
                setting.IsOnlyWifiRcv = [result boolForColumn:@"IsOnlyWifiRcv"];
                setting.BothOnline = [result boolForColumn:@"BothOnline"];
                setting.Autograph = [result stringForColumn:@"Autograph"];
                setting.userCode = [result stringForColumn:@"userCode"];
                setting.isUseGesturePassword = [result boolForColumn:@"isUseGesturePassword"];
                setting.isForgetGuesturePassword = [result boolForColumn:@"isForgetGuesturePassword"];
                setting.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
                setting.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
                setting.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            }
        }
        [result close];
    }];
    return setting;

}

- (BOOL)updateUserSetting:(CUserSettingModel *)setting
{
    if(setting == nil)
    {
        return NO;
    }
     CAccountModel *account = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"UPDATE USERSETTING SET Voice = ?, Shake = ?, Disturb = ?, StartDate = ?, EndDate = ?,BothOnline = ?,IsOnlyWifiRcv = ?,Autograph= ?,isUseGesturePassword = ?,isForgetGuesturePassword = ?, yuLiuZiduan1 = ?, yuLiuZiduan2 = ?, yuLiuZiduan3 = ? where userCode = ?", setting.Voice?@1:@0, setting.Shake?@1:@0, setting.Disturb?@1:@0, M_STOREVALUE(setting.StartDate), M_STOREVALUE(setting.EndDate),setting.BothOnline?@1:@0,setting.IsOnlyWifiRcv?@1:@0,M_STOREVALUE(setting.Autograph),setting.isUseGesturePassword?@1:@0,setting.isForgetGuesturePassword?@1:@0,M_STOREVALUE(setting.yuLiuZiduan1),@(setting.yuLiuZiduan2),M_STOREVALUE(setting.yuLiuZiduan3),M_STOREVALUE(account.userName)];
    }];
    return ret;

}

- (BOOL)delUserSetting
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from USERSETTING"];
    }];
    return ret;

}
//设置属性值
-(BOOL)setUserSettingValue:(NSString *)value  type:(USERSETTING_TYPE)type
{
    if(M_ISSTRNULL(value))
    {
        return NO;
    }
    
    NSString *key = [self getKeyByType:type];
    NSString *sql = nil;
    
    if(type == USERSETTING_AUTOGRAPH
       || type == USERSETTING_STARTDATE
       || type == USERSETTING_ENDDATE)
    {
        sql = [NSString stringWithFormat:@"Insert Or Replace Into USERSETTING(%@, identifer) VALUES (%@, 1)", key, M_STOREVALUE(value)];
    }
    else
    {
       sql = [NSString stringWithFormat:@"Insert Or Replace Into USERSETTING(%@, identifer) VALUES (%@, 1)", key,M_STOREVALUE(value)];
    }
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:sql];
        
    }];
    return ret;
}
//获取属性值
-(NSString *)getUserSettingValue:(USERSETTING_TYPE)type
{

    
    NSString *key = [self getKeyByType:type];
    
    NSString *sql = [NSString stringWithFormat:@"select %@ from USERSETTING", key];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block NSString *value = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            if(type == USERSETTING_AUTOGRAPH
               || type == USERSETTING_STARTDATE
               || type == USERSETTING_ENDDATE)
            {
                value = [result stringForColumn:key];
            }
            else
            {
                NSInteger tmp = [result intForColumn:key];
                value = [NSString stringWithFormat:@"%d", tmp];
            }
        }
        [result close];
    }];
    return value;
}


-(NSString *)getKeyByType:(USERSETTING_TYPE)type
{
    NSString *key = nil;
    
    switch (type) {
        case USERSETTING_VOICE:
            key = @"Voice";
            break;
        case USERSETTING_SHAKE:
            key = @"Shake";
            break;
        case USERSETTING_DISTURB:
            key = @"Disturb";
            break;
        case USERSETTING_STARTDATE:
            key = @"StartDate";
            break;
        case USERSETTING_ENDDATE:
            key = @"EndDate";
            break;
        case USERSETTING_BOTHONLINE:
            key = @"BothOnline";
            break;
        case USERSETTING_ISONLYWIFIRCV:
            key = @"IsOnlyWifiRcv";
            break;
        case USERSETTING_AUTOGRAPH:
            key = @"Autograph";
            break;
        default:
            break;
    }
    return key;
}
@end
